package com.wthink.newMain.settings.config.inner.basic

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.MutableLiveData
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentBasicBinding
import com.wthink.hzgj.util.toast
import com.wthink.newMain.data.BasicInfo
import kotlinx.android.synthetic.main.fragment_basic.*

class BasicFragment : Fragment() {

    val busColor = MutableLiveData("")

    private val viewModel by viewModels<BasicViewmodel>()

    private lateinit var binding: FragmentBasicBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_basic,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        /* val adapter  = ArrayAdapter.createFromResource(
           context!!,
           R.array.business,
           android.R.layout.simple_spinner_item
       )

       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
       binding.spinner.adapter = adapter*/


        binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                busColor.postValue(viewModel.colorsItems[position])
            }
        }

        viewModel.queryBusInfo()

        binding.back.setOnClickListener {

        }

        binding.commit.setOnClickListener {
            val sn = tv_factory.text.toString()
            /*   val vdrModel = viewModel.vdr_model.value
               val vdr_vendorid = viewModel.vdr_vendorid.value
               val vid = viewModel.vid.value
               val simno = viewModel.simno.value
               val car_num = viewModel.car_num.value
               val province_id = viewModel.province_id.value
               val city_id = viewModel.city_id.value
               val car_vin = viewModel.car_vin.value
               //val sn = viewModel.sn.value
               val dev_id = viewModel.dev_id.value
               val car_id = viewModel.car_id.value
               val line_id = viewModel.line_id.value
               val driver_id = viewModel.driver_id.value
               val driver_name = viewModel.driver_name.value
            */
            val vdrModel = viewModel.basicInfo.value?.vdr_model
            val vdr_vendorid = viewModel.basicInfo.value?.vdr_vendorid
            val vid = viewModel.basicInfo.value?.vid
            val simno = viewModel.basicInfo.value?.simno
            val car_num = viewModel.basicInfo.value?.car_num
            val province_id = viewModel.basicInfo.value?.province_id
            val city_id = viewModel.basicInfo.value?.city_id
            val car_vin = viewModel.basicInfo.value?.car_vin
            val dev_id = viewModel.basicInfo.value?.dev_id
            val car_id = viewModel.basicInfo.value?.car_id
            val line_id = viewModel.basicInfo.value?.line_id
            val driver_id = viewModel.basicInfo.value?.dev_id
            val driver_name = viewModel.basicInfo.value?.driver_name

            if (vdrModel!!.isEmpty() || vdr_vendorid!!.isEmpty() || vid!!.isEmpty()
                || simno!!.isEmpty() || province_id!!.isEmpty()
                || city_id!!.isEmpty() || car_vin!!.isEmpty() || sn!!.isEmpty() ||
                dev_id!!.isEmpty() || car_id!!.isEmpty() || line_id!!.isEmpty() ||
                driver_id!!.isEmpty() || driver_name!!.isEmpty()
            ) {
                toast(context!!, "请输入完整配置信息")
                return@setOnClickListener
            }

            viewModel.config(
                BasicInfo(
                    vdrModel, vdr_vendorid, vid
                    , simno, "", busColor.value!!, city_id,
                    province_id, car_vin, sn, dev_id, car_id, line_id, driver_id, driver_name
                )
            )
        }
    }
}